package com.edgar.tdd;

public class Game {

    private int score = 0;

    private int[] itsThrows = new int[21];
    private int itsCurrentThrow = 0;

    private int itsCurrentFrame;

    private boolean firstThrow;
    
    public void add(int pins) {
        itsThrows[itsCurrentThrow++] = pins;
        score += pins;
        
        adjustCurrentFrame();
    }

    private void adjustCurrentFrame() {
        if (firstThrow == true) {
            firstThrow = false;
            itsCurrentFrame ++;
        } else {
            firstThrow = true;
        }
    }

    public int score() {
        return score;
    }

    public int scoreForFrame(int frame) {
        int score = 0;
        int ball = 0;
        for (int currentFrame = 0; currentFrame < frame; currentFrame ++) {
            int firstThrow = itsThrows[ball++];
            int secondThrow = itsThrows[ball++];
            
            int frameScore = firstThrow + secondThrow;
            if (frameScore == 10) {
                score += frameScore + itsThrows[ball];
            } else {
                score += frameScore;
            }
            
        }
        
        return score;
    }

    public int getCurrentFrame() {
        return itsCurrentFrame;
    }

}
